//
//  ViewController.m
//  19-JSON
//
//  Created by dllo on 16/8/9.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "ViewController.h"
#import "LabelFlowLayout.h"
#import "NewCollectionViewCell.h"
#import "ContentTitle.h"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, waterFlowLayoutDelegate>

@property (nonatomic, strong)NSMutableArray<ContentTitle *> *contentTitleArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _contentTitleArray = [NSMutableArray array];
    [self handleData];
    [self creatCollection];
    
 }
- (void)handleData {
    // JSON解析
    // 通过一个data, 将JSON格式转换成OC认识的内容
    // 数组/字典
    NSString *data = [[NSBundle mainBundle]pathForResource:@"FirstJSON" ofType:@"json"];
    NSData *jsonData = [NSData dataWithContentsOfFile:data];
    // JSON解析
    NSError *error = nil;
    NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    NSLog(@"%@", jsonDic[@"msg"]);
    NSArray *list = jsonDic[@"tabs"][@"list"];
    for (NSDictionary *contentDic in list) {
        ContentTitle *contentTitle = [ContentTitle contentTitleWithDic:contentDic];
        [_contentTitleArray addObject:contentTitle];
    }
}

- (void)creatCollection {
    LabelFlowLayout *layout = [[LabelFlowLayout alloc] init];
    layout.delegate = self;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 50) collectionViewLayout:layout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionView];
    [collectionView registerClass:[NewCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout height:(CGFloat)height heightForItemAtIndexPath:(NSIndexPath *)indexPath {
    ContentTitle *contentTitle = _contentTitleArray[indexPath.row];
  CGRect newRect = [contentTitle.title boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 50) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
  CGFloat width = ceilf(newRect.size.width);
    
    return width;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _contentTitleArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//    cell.label.text = [_contentTitleArray[indexPath.row] title];
    cell.contentTitle = _contentTitleArray[indexPath.row];
    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
